#ifndef ABSTRACT_GRAPHIC_EDITOR_H
#define ABSTRACT_GRAPHIC_EDITOR_H

#include "../documents/swpabstractdocument.h"

#include <ieditor.h>
#include <palette.h>

#include <QtCore/QList>
#include <QtGui/QComboBox>
#include <QtGui/QFrame>

QT_BEGIN_NAMESPACE
class QGraphicsView;
class QToolBar;
QT_END_NAMESPACE

namespace core
{
class ConnectionData;
}

namespace swp
{
class RemoveConnectionCommand;
class RemoveGraphicItemCommand;

class AbstractGraphicEditor : public QFrame, public core::IEditor
{
    Q_OBJECT

public:
    AbstractGraphicEditor(QWidget* parent = 0, core::Palette* palette =
            core::Palette::defaultPalette());
    virtual ~AbstractGraphicEditor();

    // IEditor
    virtual void open(const QSharedPointer<core::IDocument>& document);
    virtual void close(const QSharedPointer<core::IDocument>& document);

    QSharedPointer<core::IDocument> document() const;
    QWidget* widget() const;

    void registerFocusListener(QObject* object);
    void unregisterFocusListener(QObject* object);

    virtual bool eventFilter(QObject *watched, QEvent *event);

protected:
    virtual RemoveConnectionCommand* removeConnectionCommand(
        core::IGraphicEditorItem* item) const;
    virtual RemoveGraphicItemCommand* removeItemCommand(
        core::IGraphicEditorItem* item) const;

private:
    void createActions();
    void connectSlots();
    void createCursors();

    void sendFocusEvent(bool focusIn = true) const;
    void cancelUnfinishedConnection(bool resetMode = false);

    int indexForScale(qreal zoomLevel) const;

protected slots:
    void setModeToEdit();
    void setModeToConnect(const core::ConnectionData& data);
    void setModeToSelect();
    void setModeToTransform();
    void setModeToZoomIn();
    void setModeToZoomOut();

private slots:
    void changeZoomLevel();
    void zoomIn(const QPointF& center = QPointF(-1, -1));
    void zoomOut(const QPointF& center = QPointF(-1, -1));
    void scaleToFitContent();
    void removeSelectedItems();
    void escapeNonDisplayableMode();
    void escapeLastAnchor();
    void refreshLinkedModels();

protected:
    QList<QObject* > m_focusListeners;

    QSharedPointer<core::IDocument> m_document;
    GraphicData* m_documentData;

    QAction* m_selectMode;
    QAction* m_transformMode;
    QAction* m_escapeNonDisplayableMode;
    QAction* m_escapeLastAnchor;
    QAction* m_zoomIn;
    QAction* m_zoomOut;
    QAction* m_zoomInMode;
    QAction* m_zoomOutMode;
    QAction* m_scaleToFitContent;
    QAction* m_removeSelected;
    QAction* m_refreshLinkedModels;

    QCursor m_connectCursor;
    QCursor m_selectCursor;
    QCursor m_transformCursor;
    QCursor m_zoomInCursor;
    QCursor m_zoomOutCursor;

    QComboBox* m_zoomLevel;

    QGraphicsView* m_view;
    QToolBar* m_toolbar;

    core::Palette* m_palette;

    qreal m_currentScale;

    bool m_isInZoomInMode;
    bool m_isInZoomOutMode;
};

} // namespace swp

#endif
